package com.mebs.model.service.visa;

import org.jt.jet.annotation.ArgumentType;
import org.jt.jet.annotation.Field;
import static org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy;

import javax.persistence.*;
import java.util.Date;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Mar 10, 2006, 6:40:45 PM
 */
@Entity()
@Table(name = "VISA_PROCESS",
    uniqueConstraints = {
    @UniqueConstraint(columnNames = {"VisaId", "ProcessActionId"}),
    @UniqueConstraint(columnNames = {"VisaId", "FollowUpActionId"})}
)
public class VisaProcess {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id")
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "VisaId")
    private Visa visa;

    @ManyToOne(optional = false)
    @JoinColumn(name = "ProcessActionId")
    private ProcessAction action;

    @Column(name = "Performer", nullable = true, length = 250)
    private String performer;

    @Column(name = "PlannedDate", nullable = true)
    private Date plannedDate;

    @Column(name = "ActualDate", nullable = true)
    private Date actualDate;

    @ManyToOne(optional = true)
    @JoinColumn(name = "FollowUpActionId")
    @Field(type = ArgumentType.INSTANCE)
    private ProcessAction followUpAction;

    @Column(name = "FollowUpDate", nullable = true)
    private Date followUpDate;


    /**
     * ********************** Getters-setters ***********************
     */

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Visa getVisa()
    {
        return visa;
    }

    public void setVisa(Visa visa)
    {
        this.visa = visa;
    }

    public ProcessAction getAction()
    {
        return action;
    }

    public void setAction(ProcessAction action)
    {
        this.action = action;
    }

    public String getPerformer()
    {
        return performer;
    }

    public void setPerformer(String performer)
    {
        this.performer = performer;
    }

    public Date getPlannedDate()
    {
        return plannedDate;
    }

    public void setPlannedDate(Date plannedDate)
    {
        this.plannedDate = plannedDate;
    }

    public Date getActualDate()
    {
        return actualDate;
    }

    public void setActualDate(Date actualDate)
    {
        this.actualDate = actualDate;
    }

    public ProcessAction getFollowUpAction()
    {
        return followUpAction;
    }

    public void setFollowUpAction(ProcessAction followUpAction)
    {
        this.followUpAction = followUpAction;
    }

    public Date getFollowUpDate()
    {
        return followUpDate;
    }

    public void setFollowUpDate(Date followUpDate)
    {
        this.followUpDate = followUpDate;
    }

    @Transient
    public boolean isDeletable()
    {
        for (VisaProcess visaProcess : this.visa.getProcessActions()) {
            if ((visaProcess.getFollowUpAction() != null) &&
                (action.getId().equals(visaProcess.getFollowUpAction().getId()))) {
                return false;
            }
        }
        return true;
    }

    @Override
    public String toString()
    {
        return (this.action == null) ? "" : this.action.toString();
    }

    @Override
    public int hashCode()
    {
        if (id == null) return super.hashCode();
        return id.hashCode();
    }

    @Override
    public boolean equals(Object o)
    {
        if (o == null) return false;
        if (this == o) return true;
        if (id == null) return false;
        if (getClassWithoutInitializingProxy(this) != getClassWithoutInitializingProxy(o))
            return false;
        final VisaProcess that = (VisaProcess) o;
        return id.equals(that.getId());
    }

}
